using System;
using InfoJetSoft.Service.Worker;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for ADOAdapter.
	/// </summary>
	public class ADOAdapter : Microsoft.Office.Interop.InfoPath.SemiTrust.ADOAdapterObject
	{

		private InfoJetSoft.Service.Entity.Form form = null;
		private InfoJetSoft.Service.Entity.DataADOAdapter adoAdapter = null;

		public ADOAdapter( InfoJetSoft.Service.Entity.Form frm, InfoJetSoft.Service.Entity.DataADOAdapter adapter )
		{
			this.form = frm;
			this.adoAdapter = (InfoJetSoft.Service.Entity.DataADOAdapter)DataAdapterTableUtil.getCurrentAdapter( this.form, adapter );
		}

		public string Name 
		{ 
			get
			{
				return this.adoAdapter.Name;
			}
		}

		public string Connection 
		{ 
			get
			{
				return this.adoAdapter.ConnectionString;
			}
			set
			{
				this.adoAdapter.ConnectionString = value;
			}
		}

		public string Command 
		{ 
			get
			{
				return this.adoAdapter.CommandText;
			}
			set
			{
				this.adoAdapter.CommandText = value;
			}
		}

		public bool QueryAllowed 
		{ 
			get
			{
				return this.adoAdapter.QueryAllowed;
			}
		}

		public bool SubmitAllowed 
		{ 
			get
			{
				return this.adoAdapter.SubmitAllowed;
			}
		}

		public void Query()
		{
			InfoJetSoft.Service.Entity.DataADOAdapter templateAdapter = (InfoJetSoft.Service.Entity.DataADOAdapter) DataAdapterTableUtil.getTemplateAdapter( this.form, this.adoAdapter );
			if( this.adoAdapter.QueryAllowed )
			{
				InfoJetSoft.Service.Entity.Query query = this.form.Manifest.Query;
				if( query != null && query.AdoAdapter != null && query.AdoAdapter.Equals( templateAdapter ) )
				{
					DataObjectDBQueryWorker.queryDoc( this.form, this.adoAdapter );
					return;
				}
				foreach( InfoJetSoft.Service.Entity.DataObject dataObject in this.form.Manifest.DataObjectList )
				{
					if( dataObject.AdoAdapter != null && dataObject.AdoAdapter.Equals( templateAdapter ) )
					{
						DataObjectDBLoadWorker.loadDBDataObject( this.form, dataObject, this.adoAdapter );
						return;
					}
				}
			}
		}
		
		//unsupported
		public string BuildSQLFromXMLNodes(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode pXmlNode)
		{
			return null;
		}

		public int Timeout 
		{ 
			get
			{
				return this.adoAdapter.Timeout;
			}
			set
			{
				this.adoAdapter.Timeout = value;
			}
		}

		public void Submit(){}

	}
}
